﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using BlawkayEngine;

namespace BlawkayHockey
{
    public class Rink
    {
        public Rink(Game oGame, float oLong, float oWide)
        {
            _Ice = new Ice(oGame, "Ice", oLong, oWide);

            _Walls = new List<Wall>();

            // Boards
            _Wall_Left = new Wall(oGame, "wall", new Vector3(0, 0, -oLong * 0.5f), VectorHelper.GLOBAL_RIGHT, oWide, 30);
            _Wall_Right = new Wall(oGame, "wall", new Vector3(0, 0, oLong * 0.5f), -VectorHelper.GLOBAL_RIGHT, oWide, 30);
            _Wall_Forward = new Wall(oGame, "wall", new Vector3(-oWide * 0.5f, 0, 0), VectorHelper.GLOBAL_BACKWARD, oLong, 30);
            _Wall_Back = new Wall(oGame, "wall", new Vector3(oWide * 0.5f, 0, 0), -VectorHelper.GLOBAL_BACKWARD, oLong, 30);

            _Walls.Add(_Wall_Left);
            _Walls.Add(_Wall_Right);
            _Walls.Add(_Wall_Forward);
            _Walls.Add(_Wall_Back);

            _GoalSouth = new Goal(oGame, new Vector3(0, 0, (oLong - 25) * 0.5f), "Ice", 25, 60, 25);
            _GoalNorth = new Goal(oGame, new Vector3(0, 0, (-oLong + 25) * 0.5f), "Ice", 25, 60, 25);
        }

        public void Update(Camera oCamera)
        {
            _Ice.Update(oCamera);
            foreach (Wall oWall in _Walls)
                oWall.Update(oCamera);

            _GoalSouth.Update(oCamera);
            _GoalNorth.Update(oCamera);

            Vector3 oLookAt = -1 * oCamera.CameraForward;
            float posX = oLookAt.X;
            float posZ = oLookAt.Z;

            if (Math.Abs(posX) > Math.Abs(posZ))
            {
                List<Wall> oWalls = new List<Wall>();
                if (posX > 0)
                {
                    oWalls.Add(_Wall_Left);
                    oWalls.Add(_Wall_Right);
                    oWalls.Add(_Wall_Forward);
                    oWalls.Add(_Wall_Back);
                }
                else
                {
                    oWalls.Add(_Wall_Right);
                    oWalls.Add(_Wall_Left);
                    oWalls.Add(_Wall_Back);
                    oWalls.Add(_Wall_Forward);
                }
                _Walls = oWalls;
            }
            else
            {
                List<Wall> oWalls = new List<Wall>();
                if (posZ > 0)
                {
                    oWalls.Add(_Wall_Back);
                    oWalls.Add(_Wall_Forward);
                    oWalls.Add(_Wall_Left);
                    oWalls.Add(_Wall_Right);
                }
                else
                {
                    oWalls.Add(_Wall_Forward);
                    oWalls.Add(_Wall_Back);
                    oWalls.Add(_Wall_Right);
                    oWalls.Add(_Wall_Left);
                }
                _Walls = oWalls;
            }
        }

        public void DrawOpaque(Game oGame)
        {
            _Ice.DrawOpaque(oGame);

            foreach (Wall oWall in _Walls)
                oWall.DrawOpaque(oGame);

            _GoalSouth.DrawOpaque(oGame);
            _GoalNorth.DrawOpaque(oGame);
        }

        public void DrawAlpha(Game oGame)
        {
            foreach (Wall oWall in _Walls)
                oWall.DrawAlpha(oGame);
        }

        Ice _Ice;

        List<Wall> _Walls;
        Wall _Wall_Right;
        Wall _Wall_Left;
        Wall _Wall_Forward;
        Wall _Wall_Back;

        Goal _GoalSouth;
        Goal _GoalNorth;
    }
}
